#include <iostream>
#include <vector>
using namespace std;

// class Solution {
// public:
//     int search(vector<int>& nums, int target) {
//         for(int i = 0;i < nums.size();++i)
//         {
//             if(nums[i] == target) return i;
//         }
//         return -1;
//     }
// };

// 二分法
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size() - 1;
        while(left <= right)
        {
            int middle = left + (right - left) / 2;
            if(nums[middle] == target) return middle;
            else if(nums[middle] < target) 
            {
                left = middle + 1;
            }
            else if(nums[middle] > target)
            {
                right = middle - 1;
            }
        }
        return -1;
    }
};

int main()
{
    vector<int> vec = {-1,0,3,5,9,12};
    int target = 2;
    Solution sol;
    cout << sol.search(vec,target) << endl;
    return 0;
}